package com.ifnk.portaljava.hlyt.dao.impl;

import com.ifnk.portaljava.dapWeb.advance.DBAccess;
import com.ifnk.portaljava.hlyt.beans.Module;
import com.ifnk.portaljava.hlyt.dao.IModuleJdbcDao;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ModuleJdbcDaoImpl implements IModuleJdbcDao {

	public List findAll() throws Exception {
		DBAccess db = new DBAccess();
		String querySql = "select id,name,location,displayname,enable,createtime,disabletime,permission,memo from module where disabletime is null order by name asc;";
		System.out.println("querySql:" + querySql);
		ResultSet rs = db.queryTable(querySql);
		List<Module> moduleList = new ArrayList<Module>();
		Module module;
		try {
			while (rs.next()) {
				module = new Module();
				module.setId(rs.getInt("id"));
				module.setName(rs.getString("name"));
				module.setLocation(rs.getString("location"));
				module.setDisplayname(rs.getString("displayname"));
				module.setEnable(rs.getInt("enable"));
				module.setCreatetime(rs.getTimestamp("createtime"));
				module.setDisabletime(rs.getDate("disabletime"));
				module.setPermission(rs.getString("permission"));
				module.setMemo(rs.getString("memo"));

				module.setCreatetimeString(new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss").format(rs
						.getTimestamp("createtime")));
				if (rs.getTimestamp("disabletime") != null) {
					module.setDisabletimeString(new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss").format(rs
							.getTimestamp("disabletime")));
				}
				moduleList.add(module);
			}
		} catch (SQLException e) {
			throw new Exception ("findAll error!");
		}finally{
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return moduleList;
	}

	public int save(Module module, Connection conn) throws Exception {
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int id = 0;
		try {
			String idSql = "select max(id) as maxId from module";
			stmt = conn.createStatement();
			rs = stmt.executeQuery(idSql);
			while (rs.next()) {
				id = rs.getInt("maxId") + 1;
			}
			String sql = "insert into module(id,name,location,displayname,enable,permission,memo,createtime) values(?,?,?,?,?,?,?,now())";
			System.out.println("insert sql:" + sql);
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			pstmt.setString(2, module.getName());
			pstmt.setString(3, module.getLocation());
			pstmt.setString(4, module.getDisplayname());
			pstmt.setInt(5, module.getEnable());
			pstmt.setString(6, module.getPermission());
			pstmt.setString(7, module.getMemo());
			pstmt.execute();
		} catch (SQLException e) {
			throw new Exception ("save module error!");
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
				if (pstmt != null) {
					pstmt.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return id;

	}

	public void update(Module module, Connection conn) throws Exception {
		PreparedStatement pstmt = null;
		try {
			String sql = "update module set name = ?,location = ?,displayname=?,enable=?,memo=? where id=?";
			System.out.println("sql:" + sql);
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, module.getName());
			pstmt.setString(2, module.getLocation());
			pstmt.setString(3, module.getDisplayname());
			pstmt.setInt(4, module.getEnable());
//			pstmt.setString(5, module.getPermission());
			pstmt.setString(5, module.getMemo());
			pstmt.setInt(6, module.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new Exception ("update module error!");
		} finally {
			try {
				if (pstmt != null) {
					pstmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	public void delete(Module module, Connection conn) throws Exception {
		PreparedStatement pstmt = null;
		try {
			String sql = "update module set name = ?,location = ?,displayname=?,enable=?,permission=?,memo=?,disabletime=now() where id=?";
			System.out.println("sql:" + sql);
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, module.getName());
			pstmt.setString(2, module.getLocation());
			pstmt.setString(3, module.getDisplayname());
			pstmt.setInt(4, module.getEnable());
			pstmt.setString(5, module.getPermission());
			pstmt.setString(6, module.getMemo());
			pstmt.setInt(7, module.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new Exception ("delete module error!");
		} finally {
			try {
				if (pstmt != null) {
					pstmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	public static void main(String[] args) throws Exception {
		Module m = new Module();
		m.setName("wwwwwwww");
		m.setLocation("roleManage/RoleManage.swf");
		m.setDisplayname("aaaaaaaaaaa");
		m.setEnable(1);
		m.setCreatetime(Calendar.getInstance().getTime());
		m.setDisabletime(null);
		m.setPermission("read;add");
		m.setMemo("wwwwwwwwwww");
		ModuleJdbcDaoImpl mjd = new ModuleJdbcDaoImpl();
		mjd.save(m, DBAccess.getConn());
		mjd.findAll();
	}

	public List findByDisplayname(String displayname) throws Exception {
		DBAccess db = new DBAccess();
		String querySql = "select id,name,location,displayname,enable,createtime,disabletime,permission,memo from module where displayname='"+displayname+"' and disabletime is null order by name asc;";
		System.out.println("querySql:" + querySql);
		ResultSet rs = db.queryTable(querySql);
		List<Module> moduleList = new ArrayList<Module>();
		Module module;
		try {
			while (rs.next()) {
				module = new Module();
				module.setId(rs.getInt("id"));
				module.setName(rs.getString("name"));
				module.setLocation(rs.getString("location"));
				module.setDisplayname(rs.getString("displayname"));
				module.setEnable(rs.getInt("enable"));
				module.setCreatetime(rs.getTimestamp("createtime"));
				module.setDisabletime(rs.getDate("disabletime"));
				module.setPermission(rs.getString("permission"));
				module.setMemo(rs.getString("memo"));

				module.setCreatetimeString(new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss").format(rs
						.getTimestamp("createtime")));
				if (rs.getTimestamp("disabletime") != null) {
					module.setDisabletimeString(new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss").format(rs
							.getTimestamp("disabletime")));
				}
				moduleList.add(module);
			}
		} catch (SQLException e) {
			throw new Exception ("findByDisplayname error!");
		}finally{
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return moduleList;
	}

}
